package com.github.tangmonkmeat;

import java.util.Stack;

/**
 * Description:
 *
 * @author zwl
 * @version 1.0
 * @date 2021/8/7 下午7:46
 */
public class 有效括号简单 {

    public static void main(String[] args) {

        boolean b = IsValidExp("}");

        System.out.println(b);

    }

    public static boolean IsValidExp (String s) {
        if(s == null || "".equals(s)){
            return true;
        }
        char[] chs = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for(char c : chs){
            if(c == '(' || c == '[' || c == '{'){
                stack.push(c);
            }else if(c == ')' && !stack.isEmpty() && stack.peek() == '('){
                stack.pop();
            }else if(c == ']' && !stack.isEmpty() && stack.peek() == '['){
                stack.pop();
            }else if (c == '}' && !stack.isEmpty() && stack.peek() == '{'){
                stack.pop();
            }else {
                return false;
            }
        }
        return stack.isEmpty();
    }
}
